package com.dq.controller.op.opboss.set;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgCustomerLevelQuery;
import com.dq.controller.in.OrgHonorWallQuery;
import com.dq.controller.in.OrgTimeQuantumQuery;
import com.dq.controller.in.RestDayRecordQuery;
import com.dq.controller.in.request.OrgHonorWallRequest;
import com.dq.controller.in.request.RestDayRecordRequest;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.org.OrgCustomerLevel;
import com.dq.domain.org.OrgHonorWall;
import com.dq.domain.org.OrgTimeQuantum;
import com.dq.domain.record.RestDayRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.setup.ReturnCommission;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.utils.SmallQrCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/1/2 19:18
 */
@Api(description = "后台门店设置")
@RestController
@RequestMapping("/sys/boss/orgSet")
public class OrgSetController extends AllService {

    @ApiOperation("设置床位数")
    @PostMapping("setOrgBedNum/{num}")
    public Vo setOrgBedNum(@PathVariable Long num)throws AdminException{

        return orgSetService.setOrgBedNum(num);
    }

    @ApiOperation("门店床位数查看")
    @GetMapping("getOrgBedNum")
    public Vo getOrgBedNum()throws AdminException{
        SysSection section = shiroService.getDept();
        Org org = dao.get(Org.class,section.getId());
        return new Vo(org.getBedNum());
    }

    @ApiOperation("门店信息修改")
    @PostMapping("edit")
    public Vo edit (@RequestBody Org orgEdit) throws AdminException{

        return orgSetService.edit(orgEdit);
    }

    @ApiOperation("门店信息查看")
    @GetMapping("getOrg")
    public Vo<Org.Vo> getOrg()throws AdminException{
        SysSection section = shiroService.getDept();
        Org org = dao.get(Org.class,section.getId());

        Org.Vo vo = (Org.Vo) org.toVO();
        // 初始分享
        vo.setInitialOrgQr(getOrgInitialQr(org));

        return new Vo(vo);
    }

    /**
     * 门店初始小程序授权登录顾客
     * @param orgId 门店ID， 门店编号后续不能修改
     */
    @ApiOperation("门店初始小程序授权登录顾客")
    @GetMapping("orgAccredit/{orgId}")
    public Vo orgAccredit(@PathVariable("orgId") Long orgId)throws AdminException{
        if(orgId == null) {
            throw new AdminException("门店ID 为空");
        }
        Org org = dao.get(Org.class, orgId);
        if(org == null || StringUtils.isBlank(org.getSectionCode())) {
            throw new AdminException("门店信息异常");
        }

        String wxQrcode = getOrgInitialQr(org);

        return new Vo(wxQrcode);
    }

    private String getOrgInitialQr(Org org) {
        WxUser user = orgService.initialOrgWxUser(org);

        // 生成二维码
        String accessToken = SmallQrCode.getAccessToken();
        return SmallQrCode.getWXQrcode(accessToken, "pages/index/index", "" + user.getUsername(), 250);
    }

    @ApiOperation("门店荣誉查看")
    @PostMapping("getOrgHonorWall")
    public Vo getOrgHonorWall(@RequestBody OrgHonorWallQuery orgHonorWallQuery)throws AdminException{
        Org org = (Org) shiroService.getUser().getSection();
        orgHonorWallQuery.format();
        Map<String, Object> query = orgHonorWallQuery.parseMap();
        if(org != null){
            query.put("sysDept_id",org.getId());
            query.remove("orgId");
        }
        if(orgHonorWallQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,orgHonorWallQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(orgHonorWallQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,orgHonorWallQuery.getEndTime());
            query.remove("endTime");
        }
        List<OrgHonorWall> orgHonorWalls = dao.find(dao.query2Hql(OrgHonorWall.class,query),query);
        Long count = dao.count(dao.count2Hql(OrgHonorWall.class,query),query);
        PageHelper.startPage(orgHonorWallQuery.getPage(),orgHonorWallQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<OrgHonorWall.Vo>) VO.toVOCollection(orgHonorWalls));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }


    @ApiOperation("添加荣誉墙")
    @PostMapping("addOrgHonorWall/{staffUserId}")
    public Vo addOrgHonorWall(@PathVariable  Long staffUserId,@RequestBody OrgHonorWallRequest orgHonorWallRequest) throws AdminException{

        return orgSetService.addOrgHonorWall(orgHonorWallRequest,staffUserId);
    }

    @ApiOperation("门店荣誉墙修改")
    @PostMapping("editOrgHonorWall/{id}/{staffUserId}")
    public Vo editOrgHonorWall(@PathVariable Long id,@PathVariable Long staffUserId,@RequestBody OrgHonorWallRequest orgHonorWallRequest) throws AdminException{

        return orgSetService.editOrgHonorWall(orgHonorWallRequest,staffUserId,id);
    }

    @ApiOperation("荣誉墙删除")
    @PostMapping("deleteOrgHonorWall/{id}")
    public Vo deleteOrgHonorWall(@PathVariable Long id) throws AdminException{

        return orgSetService.deleteOrgHonorWall(id);
    }

    @PostMapping("/restDayRecord")
    @ApiOperation("多条件检索休息日记录")
    public Vo restDayRecoedList(@RequestBody RestDayRecordQuery restDayRecordQuery)throws AdminException{
        if(null==restDayRecordQuery.getPage() || null==restDayRecordQuery.getSize()){throw new AdminException("页数和页长参数不存在"); }
        Org org = (Org) shiroService.getUser().getSection();
        List<RestDayRecord> restDayRecordList = restDayRecordService.findByWhere(restDayRecordQuery,org);
        Long totalCount = restDayRecordService.countByWhere(restDayRecordQuery,org);
        PageInfo pageInfo  = new PageInfo((List<RestDayRecord.Vo>) VO.toVOCollection(restDayRecordList));
        pageInfo.setTotal(totalCount);
        return new Vo(pageInfo);
    }

    @PostMapping("/restDayRecordEdit/{id}")
    @ApiOperation("休息日编辑")
    public Vo restDayRecordEdit(@PathVariable Long id,@RequestBody RestDayRecord restDayRecordEdit)throws AdminException{
        SysUser sysUser = shiroService.getUser();
        RestDayRecord restDayRecord = dao.get(RestDayRecord.class,id);
        if(restDayRecord == null){
            throw new AdminException("请重新选择");
        }
        restDayRecordEdit.setOrg((Org) sysUser.getSection());
        BeanUtils.copyProperties(restDayRecordEdit,restDayRecord, BeanNullUtil.getNullPropertyNames(restDayRecordEdit));
        restDayRecordRepository.save(restDayRecord);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/restDatRecordAdd")
    @ApiOperation("门店休息日新增")
    public Vo restDatRecordAdd(@RequestBody RestDayRecordRequest restDayRecordRequest)throws AdminException{
        if(StringUtils.isEmpty(restDayRecordRequest.getReason()) || null==restDayRecordRequest.getBeginTime() || null==restDayRecordRequest.getEndTime()){throw new AdminException("缺少必要的请求参数");}
        Org org = (Org) shiroService.getUser().getSection();
        RestDayRecord restDayRecord = new RestDayRecord(restDayRecordRequest.getBeginTime(),restDayRecordRequest.getEndTime(),restDayRecordRequest.getReason(),org);
        restDayRecord.setCreateTime(System.currentTimeMillis());
        restDayRecord.setCreateUser(shiroService.getUser());
        restDayRecordRepository.save(restDayRecord);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/deleteRestDay/{id}")
    @ApiOperation("删除休息日")
    public Vo deleteRestDay(@PathVariable Long id)throws AdminException{
        RestDayRecord dayRecord = dao.get(RestDayRecord.class,id);
        if(dayRecord == null){
            throw new AdminException("请重新选择");
        }
        dayRecord.setDeleted(true);
        restDayRecordRepository.save(dayRecord);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("orgTimeQuantumList")
    @ApiOperation("时间段列表")
    public Vo orgTimeQuantumList(@RequestBody OrgTimeQuantumQuery orgTimeQuantumQuery)throws AdminException{
        return orgSetService.orgTimeQuantumList(orgTimeQuantumQuery);
    }

    @PostMapping("editOrgTimeQuantum/{id}")
    @ApiOperation("时间段编辑")
    public Vo editOrgTimeQuantum(@PathVariable Long id,@RequestBody OrgTimeQuantum orgTimeQuantumEdit) throws AdminException{
        return orgSetService.editOrgTimeQuantum(orgTimeQuantumEdit,id);
    }


    @PostMapping("addOrgTimeQuantum")
    @ApiOperation("时间段添加")
    public Vo addOrgTimeQuantum(@RequestBody OrgTimeQuantum orgTimeQuantum) throws AdminException {
        return orgSetService.addOrgTimeQuantum(orgTimeQuantum);
    }

    @PostMapping("deleteOrgTimeQuantum/{id}")
    @ApiOperation("时间段删除")
    public Vo deleteOrgTimeQuantum(@PathVariable Long id) throws AdminException{

        return orgSetService.deleteOrgTimeQuantum(id);
    }

    @PostMapping("orgCustomerLevelList")
    @ApiOperation("会员等级列表")
    public Vo orgCustomerLevelList(@RequestBody OrgCustomerLevelQuery orgCustomerLevelQuery)throws AdminException{
        return orgSetService.orgCustomerLevelList(orgCustomerLevelQuery);
    }

    @PostMapping("addOrgCustomerLevel")
    @ApiOperation("添加会员等级")
    public Vo addOrgCustomerLevel(@RequestBody OrgCustomerLevel orgCustomerLevel) throws AdminException{
        return orgSetService.addOrgCustomerLevel(orgCustomerLevel);
    }

    @PostMapping("editOrgCustomerLevel/{id}")
    @ApiOperation("编辑会员等级")
    public Vo editOrgCustomerLevel(@PathVariable Long id,@RequestBody OrgCustomerLevel orgCustomerLevelEdit) throws AdminException{
        return orgSetService.editOrgCustomerLevel(id,orgCustomerLevelEdit);
    }

    @PostMapping("deleteOrgCustomerLevel/{id}")
    @ApiOperation("删除会员等级")
    public Vo deleteOrgCustomerLevel(@PathVariable Long id)throws AdminException{
        return orgSetService.deleteOrgCustomerLevel(id);
    }

    @GetMapping("getIntegalProportion/{proportionType}")
    @ApiOperation("获取比例")
    public Vo getIntegalProportion(@PathVariable ReturnCommission.ProportionType proportionType)throws AdminException{
        return orgSetService.getIntegalProportion(proportionType);
    }

    @PostMapping("setIntegalProportion/{proportionType}/{proportion}")
    @ApiOperation("编辑比例")
    public Vo setIntegalProportion(@PathVariable ReturnCommission.ProportionType proportionType, @PathVariable BigDecimal proportion)throws AdminException{
        return orgSetService.setIntegalProportion(proportionType,proportion);
    }

}
